import allure,pytest # todo:导入三方库
    # todo：导入的这个文件下的所有方法
from api_util import api_util
admin = api_util.perform_requests() # todo：实例化了一个用例执行类
data_List = []
@allure.story("客服话术测试")   # todo：allure报告的层级，最外层是stroy，当作一级目录来看
class TestCustomer():
    @pytest.fixture(scope='function',autouse=True) # todo：夹具
    @allure.feature("setup&teardown") # todo：allure报告的层级，最外层是feature，当作二级目录来看
    def setup_teardown(self):
        print("用例开始执行")
        yield
        print("用例执行结束...")
        print("数据清除完毕")
    @allure.feature("添加客服话术")  # todo：allure报告的层级，最外层是stroy，当作一级目录来看
    def test_AddCustomer(self):
        admin.reset_request_step()  # todo: 登录文件引用，默认不传是GetToken
        # todo： 创建请求方法  create_search_parts 可自定义命名。调用create_run_request方法，传入API名称、API地址、request body
        create_add_customer = \
            admin.create_run_request('POST',
                "添加客服话术", "adminapi/app/wechat/speechcraft",{"cate_id":0,"title":"测试一下你就知道","message":"我是测试两下","sort":0}) \
                .validate() \
                .assert_equal("status_code", 200)\
                .assert_equal('headers."Content-Type"', "application/json; charset=utf-8")\
                .assert_equal("body.status", 200)\
                .assert_equal("body.msg", "创建话术成功")

        # todo:   contruct_request_step  放入执行步骤，如果有多个，需要逗号隔开
        create_addcustomer_step = admin.contruct_request_step(
            create_add_customer)
        # todo：  perform_requests_and_get_last_response  放入需要执行用例，同样，如果有多个，需要逗号隔开
        res = admin.perform_requests_and_get_last_response(
            [create_addcustomer_step])
    @allure.feature("查询客户话术")
    def test_search_customer(self):
        admin.reset_request_step()
        search_custome = admin.create_run_request(
            'get',
            "查询客户话术", "adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=&title="+"测试一下你就知道"
        ) \
            .validate() \
            .assert_equal("status_code", 200) \
            .assert_equal('headers."Content-Type"', "application/json; charset=utf-8") \
            .assert_equal("body.status", 200) \
            .assert_equal("body.msg", "ok")
        search_custome_step = admin.contruct_request_step(
            search_custome)
        res = admin.perform_requests_and_get_last_response(
            [search_custome_step])
        print(res)
        id_ = res['res_data']['list'][-1]['id']
        data_List.append(id_)

    @allure.feature("删除客户话术")
    def test_remove_custome(self):
        admin.reset_request_step()
        remove_custome = \
            admin.create_run_request('delete',
                "删除用户分组", "adminapi/app/wechat/speechcraft/"+str(data_List[0])
                ) \
                .validate() \
                .assert_equal("status_code", 200) \
                .assert_equal('headers."Content-Type"', "application/json; charset=utf-8") \
                .assert_equal("body.status", 200) \
                .assert_equal("body.msg", "删除成功")
        remove_custom_step = admin.contruct_request_step(
            remove_custome)
        res = admin.perform_requests_and_get_last_response(
            [remove_custom_step])


